﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using System.Web.Security;
using System.IO;

namespace BskLeague.Controllers
{
    public class MyPlayerController : Controller
    {
        public BskLeagueDB _db = new BskLeagueDB(); 
        //
        // GET: /MyPlayer/

        public ActionResult Index()
        {
            var player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);

            return View(player);
        }

        [HttpPost]
        public ActionResult Index(Player player, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // generate file name
                string fileName = player.UserName + "Photo" + file.FileName.Substring(file.FileName.IndexOf('.'));

                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Content/images/user_photo/"), fileName);
                file.SaveAs(path);
                player.Photo = "/Content/images/user_photo/" + fileName;
            }
            _db.Entry(player).State = System.Data.EntityState.Modified;
            _db.SaveChanges();


            return View(player);
        }

        public ActionResult Communicates()
        {
            ViewBag.player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);

            var communicates = _db.Communicates.Where(c => c.Receiver == User.Identity.Name).OrderByDescending(c => c.Date).ToList();

            return View(communicates);
        }

        public ActionResult CreateCommunicate()
        {
            ViewBag.player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);
            return View();
        }
        [HttpPost]
        public ActionResult CreateCommunicate(Communicate communicate, string RL)
        {
            string[] users = RL.Split(new char[] { ';' });

            foreach (var u in users)
            {
                var com = new Communicate { Date = DateTime.Now, Receiver = u, Sender = Membership.GetUser().UserName, Content = communicate.Content };
                _db.Communicates.Add(com);
            }

            _db.SaveChanges();

            return RedirectToAction("Communicates");
        }

        public JsonResult GetTeam()
        {
            var myTeam = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name).Team;
            var actualSeason = _db.Seasons.SingleOrDefault(s => s.StatusId  < 3);
            List<String> result = new List<string>();
            if (actualSeason != null)
            {
                var players = _db.Players.Where(p => p.STPs.Any(stp => stp.SeasonId == actualSeason.SeasonId && stp.TeamId == myTeam.TeamId)).ToList();


                foreach (var p in players)
                {
                    result.Add(p.PlayerName + " " + p.PlayerSurname);
                    result.Add(p.UserName);
                }
            }

            return Json(result);
        }

        public ActionResult Licence()
        {
            ViewBag.player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);
            var licence = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name).Licence;

            return View(licence);
        }

        public ActionResult Announcements()
        {
            ViewBag.player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);
            var player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);
            var announcements = _db.AnnounceCommunicates.Include("Players").Where(a => a.Players.Any(p => p.LicenceId == player.LicenceId)).ToList();

            return View(announcements);
        }

        public ActionResult ConfirmAnnouncement(int id)
        {
            var player = _db.Players.SingleOrDefault(p => p.UserName == User.Identity.Name);

            var announcement = _db.AnnounceCommunicates.Include("Players").SingleOrDefault(a => a.AnnounceCommunicateId == id);

            announcement.Players.SingleOrDefault(p => p.LicenceId == player.LicenceId).Confirmed = true;

            _db.SaveChanges();

            return RedirectToAction("Announcements");
        }

    }
}
